#include <iostream>
#include <functional>
#include <memory>
#include <unordered_map>
#include "protocol.pb.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"
#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"

class Server
{
private:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<protocol::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<protocol::TranslateRequest> TranslateRequestPtr;
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,const MessagePtr& message,muduo::Timestamp)
    {
        LOG_INFO<<"onKnownMessage: "<<message->GetTypeName();
        conn->shutdown();
    }

    std::string translate(const std::string& str)
    {
        static std::unordered_map<std::string,std::string> dict={
            {"hello","你好"},
            {"world","世界"}
        };

        auto iter=dict.find(str);
        if(iter==dict.end())
        {
            return "不懂";
        }
        else
        {
            return iter->second;
        }
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn,const TranslateRequestPtr& message,muduo::Timestamp)
    {
        //获取数据
        std::string str=message->message();
        //翻译
        std::string rsp_str=translate(str);
        //组织响应
        protocol::TranslateResponse rsp_msg;
        rsp_msg.set_message(rsp_str);
        //发送
        _codec.send(conn,rsp_msg);//底层会进行序列化,并构建protobuf协议报文
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn,const AddRequestPtr& message,muduo::Timestamp)
    {
        int num1=message->num1();
        int num2=message->num2();
        int result=num1+num2;
        protocol::AddResponse rsp_msg;
        rsp_msg.set_result(result);
        _codec.send(conn,rsp_msg);
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected()==true)
        {
           LOG_INFO<<"新连接建立成功!";
        }
        else
        {
            LOG_INFO<<"连接即将关闭!";
        }
    }

public:
    Server(uint16_t port=8080)
        :_server(&_baseloop,muduo::net::InetAddress("0.0.0.0",port),"server",muduo::net::TcpServer::kReusePort)
        ,_dispatcher(std::bind(&Server::onUnknownMessage,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage,&_dispatcher,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3))
    {
        //给分发器设置回调函数
        _dispatcher.registerMessageCallback<protocol::TranslateRequest>(std::bind(&Server::onTranslate,this,std::placeholders::_1,
                                                                    std::placeholders::_2,std::placeholders::_3));
        _dispatcher.registerMessageCallback<protocol::AddRequest>(std::bind(&Server::onAdd,this,std::placeholders::_1,
                                                                    std::placeholders::_2,std::placeholders::_3));
        
        //给_server设置回调函数
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_codec,std::placeholders::_1,
                                            std::placeholders::_2,std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection,this,std::placeholders::_1));
    }

    void start()
    {
        _server.start();
        _baseloop.loop();
    }

    ~Server()=default;
private:
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server;
    ProtobufDispatcher _dispatcher;//请求分发器对象--要向其中注册请求处理函数
    ProtobufCodec _codec;//protobuf协议处理器--针对收到的请求数据进行protobuf协议处理
};

int main()
{
    std::unique_ptr<Server> ptr(new Server());
    ptr->start();
    return 0;
}